package com.shengqi.xinnongren.work.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.utils.ImageUtils;
import com.shengqi.xinnongren.common.utils.ToastHelper;


public class AfterSalesDialog extends Dialog {
    public AfterSalesDialog(@NonNull Context context) {
        super(context);
    }

    public AfterSalesDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
    }

    public static class Builder {
        private Context mContext;
        private String mAvatarUrl;
        private String mName;
        private String mTelephone;
        private String mContent;

        public Builder setData(String avatarUrl, String name, String telephone, String content) {
            mAvatarUrl = avatarUrl;
            mName = name;
            mTelephone = telephone;
            mContent = content;
            return this;
        }

        public Builder(Context context) {
            mContext = context;
        }

        public AfterSalesDialog build() {
            final AfterSalesDialog dialog = new AfterSalesDialog(mContext, R.style.CustomDialog);
            View layout = View.inflate(mContext, R.layout.dialog_after_sales, null);

            layout.findViewById(R.id.iv_close).setOnClickListener(v -> dialog.dismiss());
            ImageView ivAvatar = layout.findViewById(R.id.iv_avatar);
            ImageUtils.showImage(mContext, mAvatarUrl, ivAvatar);
            TextView tvName = layout.findViewById(R.id.tv_name);
            tvName.setText(mName);
            TextView tvTel = layout.findViewById(R.id.tv_tel);
            tvTel.setText(mTelephone);
            TextView tvContent = layout.findViewById(R.id.tv_content);
            tvContent.setText(mContent);
            layout.findViewById(R.id.iv_call).setOnClickListener(v -> {
                dialog.dismiss();
                if (TextUtils.isEmpty(mTelephone)) {
                    ToastHelper.getInstance(mContext).showError("尚未配置号码");
                } else {
                    try {
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        intent.setData(Uri.parse(String.format("tel:%s", mTelephone)));
                        mContext.startActivity(intent);
                    } catch (Exception e) {
                        ToastHelper.getInstance(mContext).showError(e.getMessage());
                    }
                }
            });

            //将布局添加到 dialog 中
            dialog.setContentView(layout);
            return dialog;
        }

    }
}
